;;;; Created on 2007-05-04 05:59:43



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;              FILEIO                ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; reads a parenthesis wrapped space delimited file into a two-dimensional list
(defun read-lists (file)
  (let ((lst nil))
    (with-open-file (str file :direction :input)
      (do ((line (read str nil 'eof)
                 (read str nil 'eof)))
        ((eql line 'eof))
        (setf lst (cons line lst))))
    lst))




;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;              RANDOM                ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun rands (count range)
  (let ((r (list )))
    (progn
      (dotimes (i count)
        (setf r (cons (random range) r)))
      r)))

(defun randbin (count)
  (if (> count 1)
    (cons (random 10) (randbin (- count 1)))
    (cons (random 10) nil)))

(defun rand-normal (mean-val standard-deviation)
  (let* ((theta (random (* 2. pi)))
         (r (sqrt (* -2.0 (log (random 1.0))))))
    (+ mean-val (* standard-deviation r (cos theta)))))

(defun rands-normal (mean-val standard-deviation count &optional result)
  (if (> count 0)
      (rands-normal 
        mean-val
        standard-deviation 
        (1- count) 
        (cons (rand-normal mean-val standard-deviation) result))
      result))

(defun d-rands-normal (mean-val standard-deviation count dimensions &optional result)
  (if (> count 0)
      (d-rands-normal mean-val standard-deviation (1- count) dimensions
                     (cons (rands-normal mean-val standard-deviation dimensions) result))
      result))


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;            LIST-TOOLS              ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

;; Randomly takes an element from a list
(defun random-elt (choices)
  (elt choices (random (length choices))))

;; Folds the lst into folds the size of n
(defun nfold (lst n)
  (let ((dlst lst) (folds (list )))
    (loop
      (when (= (length dlst) 0)
        (return folds))
      (setf folds (cons (list ) folds))
      (dotimes (i n)
        (when (= (length dlst) 0)
          (return folds))
        (setf (car folds) (cons (random-elt dlst) (car folds)))
        (setf dlst (remove (car (car folds)) dlst))))))


;; Folds the lst into folds the size of n and conses the fold with the not folds
(defun nfold-with-set (lst n)
  (let ((folds (nfold lst n)) (dlst nil))
    (progn
      (dotimes (i (length folds))
        (setf dlst lst)
        (dolist (val (nth i folds))
          (setf dlst (remove val dlst)))
        (setf (nth i folds) (cons (nth i folds) dlst)))
      folds)))

;; Folds the list into portions, so 10 portions, portion = 0.1
(defun portion-nfold (lst portion)
  (nfold lst (ceiling (* (length lst) portion))))

;; Folds the list into portions with not in set conses, so 10 portions, portion = 0.1
(defun portion-nfold-with-set (lst portion)
  (nfold-with-set lst (ceiling (* (length lst) portion))))

(defun invert-list (lst)
  (let ((r nil))
    (progn
      (dotimes (i (length (car lst)))
        (setf r (cons (list ) r)))
      (dolist (sub lst)
        (dotimes (i (length sub))
          (setf (nth i r) (cons (nth i sub) (nth i r)))))
      r)))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;               STATS                ;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun sum (lst)
  (let ((total 0))
    (progn
      (dolist (val lst)
          (if (numberp val)
            (setf total (+ val total))))
      total)))

(defun mean (lst)
  (/ (sum lst) (length lst)))


(defun nth-mean (lst val)
  (mean (nth val (invert-list lst))))

;;(defun d-mean (lst)
;;  (mapcar #'mean (invert-list lst)))

(defun stddev (lst)
  (let ((m (mean lst)) (dev 0))
    (progn
      (dolist (val lst)
        (if (numberp val)
          (setf dev (+ dev (abs (- val m))))))
      (/ dev (length lst)))))

(defun variance (lst)
  (let ((m (mean lst)) (dev 0))
    (progn
      (dolist (val lst)
        (if (numberp val)
          (setf dev (+ dev (expt (- val m) 2)))))
      (/ dev (length lst)))))


(defun stddev-list (lst)
  (let ((devs nil))
    (progn
      (dolist (cur lst)
        (setf devs (cons (stddev cur) devs)))
      (reverse devs))))
